package com.yyzw.activiti.modeler.controller;


import com.yyzw.activiti.modeler.model.*;
import com.yyzw.activiti.modeler.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/home")
public class HomeController {

    @Autowired
    MenuService menuService;

    List<Map<String,Object>> comboTreeList  =new ArrayList<Map<String,Object>>();


    @RequestMapping(value = "/homePage")
    public ModelAndView homePage(ModelAndView modelAndView){
        modelAndView.setViewName("main/home_page");
        return modelAndView;
    }


    @RequestMapping("getMenuList")
    public List<OrganizationVo> getMenuList(HttpServletRequest request) throws Exception{

        SysUser user =(SysUser)request.getSession().getAttribute("userSession");
        List<SysMenu> list = new ArrayList<>();
        list = menuService.getAdminMenu();
       // if(user==null||user.getUserCode().equals("admin")){

       /* }else{
            list = menuService.getUserMenuByRoleId(user.getRoleId());
        }*/
        /*List<TreeDataVo> resultList = new ArrayList<>();
        for (SysMenu sysMenu:list) {
            TreeDataVo treeDataVo = new TreeDataVo();
            treeDataVo.setId(sysMenu.getMenuId());
            treeDataVo.setText(sysMenu.getMenuName());
            treeDataVo.setState(sysMenu.getLevel().equals("1") ? "open" : "closed");
            treeDataVo.setParentId(sysMenu.getParentId());
            treeDataVo.setHref(sysMenu.getMenuUrl());
            treeDataVo.setTarget(sysMenu.getLevel().equals("1") ? "false":"true");
            resultList.add(treeDataVo);
        }
        return  genTree(resultList);*/
        return getFatherNode(list);
    }

    public List<TreeDataVo> genTree(List<TreeDataVo> list){
        List<TreeDataVo> nodeList = new ArrayList<TreeDataVo>();
        for(TreeDataVo node1:list){
            boolean mark = false;
            for (TreeDataVo node2:list){
                if(node1.getParentId() == node2.getId()){
                    mark=true;
                    if(node2.getChildren()==null){
                        node2.setChildren(new ArrayList<>());
                    }
                    node2.getChildren().add(node1);
                    break;
                }
            }
            if(!mark){
                nodeList.add(node1);
            }
        }
        return nodeList;
    }


    @RequestMapping("getMenuTree")
    public List<Map<String,Object>>  getMenuTree(HttpServletRequest request){
        List<SysMenu> menus = menuService.getAdminMenu();
        createComboTreeTree(menus,0);
        return comboTreeList;
    }

    public void createComboTreeTree(List<SysMenu> list,int fid){
        for (int i=0;i<list.size();i++) {
            Map<String,Object> map =null;
            SysMenu role=(SysMenu) list.get(i);
            if(role.getParentId()==fid){
                map = new HashMap<>();
                map.put("id",list.get(i).getMenuId());
                map.put("text",list.get(i).getMenuName());
                map.put("children",createComboTreeChildren(list,role.getMenuId()));
            }
            if(map!=null){
                comboTreeList.add(map);
            }
        }
    }

    public List<Map<String,Object>> createComboTreeChildren(List<SysMenu> list,int fid){
        List<Map<String, Object>> childList = new ArrayList<Map<String, Object>>();
        for (int j = 0; j < list.size(); j++) {
            Map<String, Object> map = null;
            SysMenu treeChild = (SysMenu) list.get(j);
            if (treeChild.getParentId()==fid) {
                map = new HashMap<String, Object>();
                //这里必须要将对象角色的id、name转换成ComboTree在页面的显示形式id、text
                //ComboTree,不是数据表格，没有在页面通过columns转换数据的属性
                map.put("id", list.get(j).getMenuId());
                map.put("text", list.get(j).getMenuName());
                map.put("children", createComboTreeChildren(list, treeChild.getMenuId()));
            }

            if (map != null)
                childList.add(map);
        }
        return childList;
    }


    /**
     * 获取父节点菜单
     * @param treesList 所有树菜单集合
     * @return
     */
    public  List<OrganizationVo> getFatherNode(List<SysMenu> treesList){
        List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>();

        for (SysMenu sysMenu : treesList) {
            if (sysMenu.getParentId()>=0) {//如果pId为空，则该节点为父节点
                //递归获取父节点下的子节点
                OrganizationVo newTree = new OrganizationVo();
                newTree.setId(sysMenu.getMenuId());
                newTree.setText(sysMenu.getMenuName());
                newTree.setHref(sysMenu.getMenuUrl());
                newTree.setPid(sysMenu.getParentId());
                List<SysMenu> treesLists = menuService.getMenuNodeByPid(sysMenu.getMenuId());
                newTree.setChildren(getChildrenNode(sysMenu.getMenuId(), treesLists));
                if(treesLists.size()>0){
                    newTrees.add(newTree);
                }
            }
        }
        return newTrees;
    }

    /**
     * 递归获取子节点下的子节点
     * @param pId 父节点的ID
     * @param treesLists 所有菜单树集合
     * @return
     */
    private  List<OrganizationVo> getChildrenNode(Integer pId, List<SysMenu> treesLists){
        List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>();

        for (SysMenu mt : treesLists) {
            if (mt.getParentId()>=0 ) {
                if (mt.getParentId() == pId) {
                    OrganizationVo newTree = new OrganizationVo();
                    //递归获取子节点下的子节点，即设置树控件中的children
                    newTree.setChildren(getChildrenNode(mt.getMenuId(), menuService.getMenuNodeByPid(mt.getMenuId())));
                    //设置树控件attributes属性的数据
                    newTree.setId(mt.getMenuId());
                    newTree.setText(mt.getMenuName());
                    newTree.setPid(mt.getParentId());
                    newTree.setHref(mt.getMenuUrl());
                    newTrees.add(newTree);
                }
            }
        }
        return newTrees;
    }
}
